<?php
/**
 * 后台ActiveForm
 * 基本复制了 \yii\bootstrap\ActiveForm的代码
 */

namespace common\widgets;

use yii;
use yii\helpers;
use yii\helpers\Html;
use yii\base\InvalidConfigException;
use yii\helpers\ArrayHelper;

class ActiveForm extends \yii\widgets\ActiveForm {

    public $fieldClass = 'common\widgets\ActiveField';

    public $options = ['role' => 'form', 'data-pjax'=>true];

    public $layout = 'horizontal';

    public $enableAjaxValidation = true;

    public $title = '';

    /*
     * 表格类型
     * ['delete']
     */
    public $type;


    /**
     * @inheritdoc
     */
    public function init()
    {
        $this->validationUrl = !empty($this->validationUrl) ? $this->validationUrl : helpers\Url::to(['ajax-validate']);
        if (!in_array($this->layout, ['default', 'horizontal', 'inline'])) {
            throw new InvalidConfigException('Invalid layout type: ' . $this->layout);
        }

        echo '<h3>'.$this->title.'</h3>';

        if ($this->layout !== 'default') {
            Html::addCssClass($this->options, 'form-' . $this->layout);
        }
        parent::init();
    }


    public function run(){
        if($this->layout == 'horizontal'){
            if($this->type == 'delete'){
                echo "<div class='form-group'><div class='row'>
                        <div class='col-xs-12 col-md-12 col-sm-12'>
                            <a href='javascript:window.history.go(-1)' class='btn btn-lg btn-default'>取 消</a>&nbsp; &nbsp; &nbsp;
                            <button type='submit' class='btn btn-lg btn-primary'>确 定</button>
                        </div></div>
                      </div>";
            }else{
                echo "<div class='form-actions'><div class='row'>
                            <div class='col-md-offset-4 col-md-8'>
                                <button type='reset' class='btn btn-lg btn-default'>重 置</button>&nbsp; &nbsp; &nbsp;
                                <button type='submit' class='btn btn-lg btn-primary'>确 定</button>
                            </div></div>
                      </div>";
            }
        }
        parent::run();
    }


    public function field($model, $attribute, $options = [])
    {
        $config = $this->fieldConfig;
        if ($config instanceof \Closure) {
            $config = call_user_func($config, $model, $attribute);
        }
        if (!isset($config['class'])) {
            $config['class'] = $this->fieldClass;
        }
        return Yii::createObject(ArrayHelper::merge($config, $options, [
            'model' => $model,
            'attribute' => $attribute,
            'form' => $this,
        ]));
    }

}